package com.xzx.paymentdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.xzx.paymentdemo.entity.OrderInfo;
import com.xzx.paymentdemo.enums.OrderStatus;
import com.xzx.paymentdemo.mapper.OrderInfoMapper;
import com.xzx.paymentdemo.service.OrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzx.paymentdemo.util.OrderNoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Duration;
import java.time.Instant;
import java.util.List;

@Service
@Slf4j
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {


    @Override
    public OrderInfo createOrderByProductId(Long productId) {
        OrderInfo orderInfo = getNoPayOrderByProductId(productId);
        if (orderInfo != null) {
            return orderInfo;
        }
        orderInfo = new OrderInfo();
        orderInfo.setTitle("test")
                .setOrderNo(OrderNoUtils.getOrderNo())
                .setProductId(productId)
                .setTotalFee(1)
                .setOrderStatus(OrderStatus.NOTPAY.getType());
        boolean save = save(orderInfo);
        System.out.println(save);
        return orderInfo;
    }

    @Override
    public void saveCodeUrl(String orderNo, String codeUrl) {
        LambdaQueryWrapper<OrderInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderInfo::getOrderNo, orderNo);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCodeUrl(codeUrl);
        update(orderInfo, wrapper);
    }

    @Override
    public List<OrderInfo> listOrderByCreateTimeDesc() {
        return lambdaQuery().orderByDesc(OrderInfo::getCreateTime).list();
    }

    /**
     * 更新订单状态
     *
     * @param orderNo
     * @param orderStatus
     */
    @Override
    public void updateStatusByOrderNo(String orderNo, OrderStatus orderStatus) {
        log.info("更新订单状态 ===> {}", orderStatus.getType());
        LambdaQueryWrapper<OrderInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderInfo::getOrderNo, orderNo);
        OrderInfo orderInfo = new OrderInfo().setOrderStatus(orderStatus.getType());
        update(orderInfo, wrapper);
    }

    @Override
    public String getOrderStatus(String orderNo) {
        OrderInfo orderInfo = lambdaQuery().eq(OrderInfo::getOrderNo, orderNo).one();
        if (orderInfo == null) {
            return null;
        }
        return orderInfo.getOrderStatus();
    }

    /**
     * 从第0秒开始每隔30秒执行1次，查询创建超过5分钟，并且未支付的订单
     *
     * @param minutes
     * @return
     */
    @Override
    public List<OrderInfo> getNoPayOrderByDurations(int minutes) {
        //minutes分钟之前的时间
        Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));
        List<OrderInfo> orderInfoList = lambdaQuery().eq(OrderInfo::getOrderStatus, OrderStatus.NOTPAY.getType())
                .le(OrderInfo::getCreateTime, instant)
                .list();
        return orderInfoList;
    }

    @Override
    public OrderInfo getOrderByOrderNo(String orderNo) {
        return lambdaQuery().eq(OrderInfo::getOrderNo, orderNo).one();
    }


    private OrderInfo getNoPayOrderByProductId(Long productId) {
        return lambdaQuery().eq(OrderInfo::getProductId, productId)
                .eq(OrderInfo::getOrderStatus, OrderStatus.NOTPAY.getType()).one();
    }
}
